/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ssau.view;

import ru.ssau.lib.GBC;
import ru.ssau.lib.AdvancedJComboBox;
import ru.ssau.lib.StringIntegerPair;
import ru.ssau.entities.Message;
import ru.ssau.entities.User;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import ru.ssau.presenter.ClientPresenter;

/**
 *
 * @author Дима
 */
public class ManagerFrame extends JFrame implements ForumView {

    private AdvancedJComboBox usersComboBox;
    private JButton newUserButton;
    private JButton deleteUserButton;
    private JButton editUserButton;
    private JLabel userRegDateLabel;
    private JLabel loginLabel;
    private JLabel passwordLabel;
    private JTextArea userInfoTextArea;
    private AdvancedJComboBox threadsComboBox;
    private JButton newThreadButton;
    private JButton deleteThreadButton;
    private JButton editThreadButton;
    private JLabel threadIdLabel;
    private JLabel threadCaptionLabel;
    private AdvancedJComboBox messagesComboBox;
    private JButton newMessageButton;
    private JButton deleteMessageButton;
    private JButton editMessageButton;
    private JLabel messageThreadIdLabel;
    private JLabel messageUserIdLabel;
    private JLabel messagePostDateLabel;
    private JTextArea messageTextArea;
    private UserDialog userDialog;
    private ThreadDialog threadDialog;
    private MessageDialog messageDialog;
    
    private JFileChooser chooser;
    private ConnectionDialog connectionDialog;

    private volatile ClientPresenter presenter;

    public ManagerFrame() {
        setTitle("Manager frame");

        initComponents();
        layoutComponents();

        createMenu();

        connectToServer();

        addWindowListener(new WindowAdapter() {
            public void windowClosing() {
                try {
                    presenter.closeConnection();
                } catch(IOException e) {
                    e.printStackTrace();
                } finally {
                    System.exit(0);
                }
            }
        });

        pack();
    }

    private void initComponents() {
        connectionDialog = new ConnectionDialog(this);

        /*Initialization of userPanel elements*/
        usersComboBox = new AdvancedJComboBox();
        usersComboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent event) {
                try {
                    if(event.getStateChange() == ItemEvent.SELECTED) {
                        int id = ((StringIntegerPair) event.getItem()).getNumber();
                        presenter.setCurrentUser(id);
                    }
                } catch(IOException e) {
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        newUserButton = new JButton("New user");
        newUserButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    Boolean canChangeModel = presenter.getCanChangeModel();
                    if(canChangeModel) {
                        User user = userDialog.showDialog(null);
                        presenter.addUser(user);
                    } else {
                        JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Another client is editing model.\nTry later.", "Error", JOptionPane.INFORMATION_MESSAGE);
                    }
                } catch(IOException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                } catch(InterruptedException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Непонятно как, но это произошло", "Поток прерван", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        deleteUserButton = new JButton("Delete user");
        deleteUserButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    if(usersComboBox.getSelectedItem() == null) return;
                    Boolean canChangeModel = presenter.getCanChangeModel();
                    if(canChangeModel) {
                        int id = ((StringIntegerPair) usersComboBox.getSelectedItem()).getNumber();
                        presenter.deleteUserById(id);
                    } else {
                        JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Another client is editing model.\nTry later.", "Error", JOptionPane.INFORMATION_MESSAGE);
                    }
                } catch(IOException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                } catch(InterruptedException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Непонятно как, но это произошло", "Поток прерван", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        loginLabel = new JLabel("", JLabel.LEFT);
        passwordLabel = new JLabel("", JLabel.LEFT);
        editUserButton = new JButton("Edit user");
        editUserButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    if(usersComboBox.getSelectedItem() == null) return;
                    Boolean canChangeModel = presenter.getCanChangeModel();
                    if(canChangeModel) {
                        StringIntegerPair currentElement = (StringIntegerPair) usersComboBox.getSelectedItem();
                        int id = currentElement.getNumber();
                        User currentUser =  presenter.getUserById(id);
                        User newUser = userDialog.showDialog(currentUser);
                        presenter.replaceUser(newUser);
                    } else {
                        JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Another client is editing model.\nTry later.", "Error", JOptionPane.INFORMATION_MESSAGE);
                    }
                } catch(IOException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                } catch(InterruptedException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Непонятно как, но это произошло", "Поток прерван", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        userRegDateLabel = new JLabel("", JLabel.LEFT);
        userInfoTextArea = new JTextArea(1,15);
        userInfoTextArea.setLineWrap(true);
        userInfoTextArea.setEditable(false);

        /*Initialization of messagePanel elements*/
        newMessageButton = new JButton("New message");
        newMessageButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    Boolean canChangeModel = presenter.getCanChangeModel();
                    if(canChangeModel) {
                        Object[] users = usersComboBox.getAllItems();
                        Object[] threads = threadsComboBox.getAllItems();
                        Message message = messageDialog.showDialog(null, users, threads);
                        presenter.addMessage(message);
                    } else {
                        JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Another client is editing model.\nTry later.", "Error", JOptionPane.INFORMATION_MESSAGE);
                    }
                } catch(IOException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                } catch(InterruptedException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Непонятно как, но это произошло, но поток прерван", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        deleteMessageButton = new JButton("Delete message");
        deleteMessageButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    if(messagesComboBox.getSelectedItem() == null) return;
                    Boolean canChangeModel = presenter.getCanChangeModel();
                    if(canChangeModel) {
                        int id = ((StringIntegerPair) messagesComboBox.getSelectedItem()).getNumber();
                        presenter.deleteMessageById(id);
                    } else {
                        JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Another client is editing model.\nTry later.", "Error", JOptionPane.INFORMATION_MESSAGE);
                    }
                } catch(IOException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                } catch(InterruptedException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Непонятно как, но это произошло", "Поток прерван", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        editMessageButton = new JButton("Edit message");
        editMessageButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    if(messagesComboBox.getSelectedItem() == null) return;
                    Boolean canChangeModel = presenter.getCanChangeModel();
                    if(canChangeModel) {
                        StringIntegerPair p = (StringIntegerPair) messagesComboBox.getSelectedItem();
                        int id = p.getNumber();
                        Message currentMessage =  presenter.getMessageById(id);
                        Object[] users = usersComboBox.getAllItems();
                        Object[] threads = threadsComboBox.getAllItems();
                        Message newMessage = messageDialog.showDialog(currentMessage, users, threads);
                        presenter.replaceMessage(newMessage);
                    } else {
                        JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Another client is editing model.\nTry later.", "Error", JOptionPane.INFORMATION_MESSAGE);
                    }
                } catch(IOException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                } catch(InterruptedException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Непонятно как, но это произошло", "Поток прерван", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        messagesComboBox = new AdvancedJComboBox();
        messagesComboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent event) {
                try {
                    if(event.getStateChange() == ItemEvent.SELECTED) {
                        int id = ((StringIntegerPair) event.getItem()).getNumber();
                        presenter.setCurrentMessage(id);
                    }
                } catch(IOException e) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                }

            }
        });

        messageUserIdLabel = new JLabel("");
        messageThreadIdLabel = new JLabel("");
        messagePostDateLabel = new JLabel("", JLabel.LEFT);
        messageTextArea = new JTextArea(1,15);
        messageTextArea.setLineWrap(true);
        messageTextArea.setEditable(false);

        /*Initialization of threadPanel elements*/
        threadsComboBox = new AdvancedJComboBox();
        threadsComboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent event) {
                try {
                    if(event.getStateChange() == ItemEvent.SELECTED) {
                        int id = ((StringIntegerPair) event.getItem()).getNumber();
                        presenter.setCurrentThread(id);
                    }
                } catch(IOException e) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            "IO error", "Error", JOptionPane.ERROR_MESSAGE);
                }

            }
        });

        newThreadButton = new JButton("New thread");
        newThreadButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    Boolean canChangeModel = presenter.getCanChangeModel();
                    if(canChangeModel) {
                        ru.ssau.entities.Thread thread = threadDialog.showDialog(null);
                        presenter.addThread(thread);
                    } else {
                        JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Another client is editing model.\nTry later.", "Error", JOptionPane.INFORMATION_MESSAGE);
                    }
                } catch(IOException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            "IO error", "Error", JOptionPane.ERROR_MESSAGE);
                } catch(InterruptedException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Непонятно как, но это произошло", "Поток прерван", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        deleteThreadButton = new JButton("Delete thread");

        deleteThreadButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    if(threadsComboBox.getSelectedItem() == null) return;
                    Boolean canChangeModel = presenter.getCanChangeModel();
                    if(canChangeModel) {
                        int id = ((StringIntegerPair) threadsComboBox.getSelectedItem()).getNumber();
                        presenter.deleteThreadById(id);
                    } else {
                        JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Another client is editing model.\nTry later.", "Error", JOptionPane.INFORMATION_MESSAGE);
                    }
                } catch(IOException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                } catch(InterruptedException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Непонятно как, но это произошло", "Поток прерван", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        threadIdLabel = new JLabel("");
        editThreadButton = new JButton("Edit thread");
        editThreadButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {

                    if(threadsComboBox.getSelectedItem() == null) return;
                    Boolean canChangeModel = presenter.getCanChangeModel();
                    if(canChangeModel) {
                        StringIntegerPair currentElement = (StringIntegerPair) threadsComboBox.getSelectedItem();
                        int id = currentElement.getNumber();
                        ru.ssau.entities.Thread currentThread = presenter.getThreadById(id);
                        ru.ssau.entities.Thread newThread = threadDialog.showDialog(currentThread);
                        presenter.replaceThread(newThread);
                    } else {
                        JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Another client is editing model.\nTry later.", "Error", JOptionPane.INFORMATION_MESSAGE);
                    }
                } catch(IOException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                } catch(InterruptedException ex) {
                    JOptionPane.showMessageDialog(ManagerFrame.this,
                            "Непонятно как, но это произошло", "Поток прерван", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        threadCaptionLabel = new JLabel("");

        userDialog = new UserDialog(this);
        messageDialog = new MessageDialog(this);
        threadDialog = new ThreadDialog(this);
    }

    private void layoutComponents() {
        JPanel mainPanel = new JPanel();
        add(new JScrollPane(mainPanel));

        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));

        /*Layout userPanel*/
        JPanel userPanel = new JPanel(new GridBagLayout());
        mainPanel.add(userPanel);

        Border uBorder = BorderFactory.createEtchedBorder();
        userPanel.setBorder(BorderFactory.createTitledBorder(uBorder, "Users"));

        JLabel usersLabel = new JLabel("Users:", JLabel.RIGHT);
        userPanel.add(usersLabel, new GBC(0, 0).setInsets(0, 5, 15, 5));
        userPanel.add(usersComboBox, new GBC(1, 0).setInsets(0, 5, 5, 5).setIpad(80, 0).setWeight(80, 0).setFill(GBC.HORIZONTAL));
        userPanel.add(newUserButton, new GBC(2, 0).setInsets(0, 5, 5, 5).setWeight(10, 0));
        userPanel.add(deleteUserButton, new GBC(3, 0).setInsets(0, 5, 5, 10).setWeight(10, 0));
        JLabel regDateLabel = new JLabel("Reg. date:", JLabel.RIGHT);
        userPanel.add(regDateLabel, new GBC(0, 1).setInsets(2, 0, 5, 5));
        userPanel.add(userRegDateLabel, new GBC(1,1,2,1).setInsets(2, 0, 5, 5).setFill(GBC.HORIZONTAL));
        userPanel.add(editUserButton, new GBC(3, 1).setInsets(2, 0, 5, 5).setWeight(10, 0));
        JLabel login = new JLabel("Login:", JLabel.RIGHT);
        userPanel.add(login, new GBC(0,2).setInsets(0,5,2,5));
        userPanel.add(loginLabel, new GBC(1,2).setInsets(0,5,0,5).setFill(GBC.HORIZONTAL));
        JLabel password = new JLabel("Password:", JLabel.RIGHT);
        userPanel.add(password, new GBC(2,2).setInsets(0,5,0,5));
        userPanel.add(passwordLabel, new GBC(3,2).setInsets(0,5,0,5).setFill(GBC.HORIZONTAL));
        JLabel userInfoLabel = new JLabel("User info:", JLabel.RIGHT);
        userPanel.add(userInfoLabel, new GBC(0, 3).setInsets(5, 2, 10, 5).setFill(GBC.HORIZONTAL));
        JScrollPane sp = new JScrollPane(userInfoTextArea);
        //sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        userPanel.add(sp/*new JScrollPane(userInfoTextArea)*/, new GBC(1, 3, 3, 2).setWeight(50, 3).setInsets(5, 5, 5, 5).setIpad(0, 50).setFill(GBC.BOTH));

        /*Layout messagePanel*/
        JPanel messagePanel = new JPanel(new GridBagLayout());
        mainPanel.add(messagePanel);

        Border mBorder = BorderFactory.createEtchedBorder();
        messagePanel.setBorder(BorderFactory.createTitledBorder(mBorder, "Messages"));

        JLabel message = new JLabel("Message:");
        messagePanel.add(message, new GBC(0, 0).setInsets(20, 5, 10, 5));
        messagePanel.add(newMessageButton, new GBC(1, 0).setInsets(0, 5, 20, 5).setWeight(40, 0));
        messagePanel.add(deleteMessageButton, new GBC(2, 0).setInsets(0, 5, 5, 5).setWeight(30, 0));
        messagePanel.add(editMessageButton, new GBC(3, 0).setInsets(0, 5, 5, 5).setWeight(30, 0));
        messagePanel.add(messagesComboBox, new GBC(0, 1, 2, 1).setInsets(5, 5, 10, 5).setWeight(40, 0).setIpad(80, 0).setFill(GBC.HORIZONTAL));
        JLabel mUserId = new JLabel("User ID:", JLabel.RIGHT);
        messagePanel.add(mUserId, new GBC(0, 2).setInsets(5).setFill(GBC.HORIZONTAL));
        messagePanel.add(messageUserIdLabel, new GBC(1, 2).setInsets(5).setFill(GBC.HORIZONTAL));
        JLabel threadId = new JLabel("Thread ID:", JLabel.RIGHT);
        messagePanel.add(threadId, new GBC(2, 2).setInsets(5, 5, 10, 10).setFill(GBC.HORIZONTAL));
        messagePanel.add(messageThreadIdLabel, new GBC(3, 2).setInsets(5).setFill(GBC.HORIZONTAL));
        JLabel postDate = new JLabel("Post date:", JLabel.RIGHT);
        messagePanel.add(postDate, new GBC(0, 3).setInsets(5).setFill(GBC.HORIZONTAL));
        messagePanel.add(messagePostDateLabel, new GBC(1, 3, 3, 1).setInsets(5).setFill(GBC.HORIZONTAL));
        JLabel messageText = new JLabel("Text:", JLabel.RIGHT);
        messagePanel.add(messageText, new GBC(0, 4).setInsets(5).setFill(GBC.HORIZONTAL));
        messagePanel.add(new JScrollPane(messageTextArea), new GBC(1, 4, 3, 2).setInsets(5).setWeight(60, 80).setFill(GBC.BOTH).setIpad(0, 50));

        /*Layout threadPanel*/
        JPanel threadPanel = new JPanel(new GridBagLayout());
        mainPanel.add(threadPanel);

        Border border = BorderFactory.createEtchedBorder();
        threadPanel.setBorder(BorderFactory.createTitledBorder(border, "Threads"));

        JLabel thread = new JLabel("Thread:", JLabel.RIGHT);
        threadPanel.add(thread, new GBC(0, 0).setInsets(0, 5, 5, 5).setFill(GBC.HORIZONTAL));
        threadPanel.add(threadsComboBox, new GBC(1, 0).setInsets(0, 5, 5, 5).setIpad(80, 0).setWeight(80, 0).setFill(GBC.HORIZONTAL));
        threadPanel.add(newThreadButton, new GBC(2, 0).setInsets(0, 5, 5, 5).setWeight(10, 0));
        threadPanel.add(deleteThreadButton, new GBC(3, 0).setInsets(0, 5, 5, 5).setWeight(10, 0));
        JLabel idLabel = new JLabel("ID:", JLabel.RIGHT);
        threadPanel.add(idLabel, new GBC(0, 1).setInsets(5, 5, 10, 5).setFill(GBC.HORIZONTAL));
        threadPanel.add(threadIdLabel, new GBC(1, 1, 2, 1).setInsets(5, 5, 5, 10).setWeight(90, 0).setFill(GBC.HORIZONTAL));
        threadPanel.add(editThreadButton, new GBC(3, 1).setInsets(5).setWeight(10, 0));
        JLabel captionLabel = new JLabel("Caption:", JLabel.RIGHT);
        threadPanel.add(captionLabel, new GBC(0, 2).setInsets(5).setFill(GBC.HORIZONTAL));
        threadPanel.add(threadCaptionLabel, new GBC(1, 2, 3, 1).setInsets(5).setFill(GBC.HORIZONTAL));

    }

    private void connectToServer() {
        boolean isConnected = false;
        while(!isConnected) {
            connectionDialog.showDialog();
            try {
                presenter = new ClientPresenter(connectionDialog.getHost(),
                        connectionDialog.getPort(), this);
                isConnected = true;

                Thread t = new Thread(new Runnable() {
                    public void run() {
                        presenter.service();
                    }
                });
                t.start();
            } catch(java.net.ConnectException e) {
                JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void createMenu() {
        /*Creating menu*/
        //Не густо, но все же...
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);

        JMenuItem exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    presenter.closeConnection();
                } catch(IOException ex) {
                    ex.printStackTrace();
                } finally {
                    System.exit(0);
                }
            }
        });
        fileMenu.add(exitItem);
    }


    //Следующие методы - реализация интерфейса ForumView, предназначенного для
    //общения презентера с представлением
    public void setCurrentUser(User user) {
        if(user == null) {
            userInfoTextArea.setText("");
            userRegDateLabel.setText("");
            loginLabel.setText("");
            passwordLabel.setText("");
        } else {
            userInfoTextArea.setText(user.getInfo());
            userRegDateLabel.setText(user.getRegistrationDate().toString());
            loginLabel.setText(user.getAuthorizationInfo().getLogin());
            passwordLabel.setText(user.getAuthorizationInfo().getPassword());
        }
    }

    public void setCurrentThread(ru.ssau.entities.Thread thread) {
        if(thread == null) {
            threadCaptionLabel.setText("");
            threadIdLabel.setText("");
        } else {
            threadCaptionLabel.setText(thread.getCaption());
            threadIdLabel.setText("" + thread.getId());
        }
    }

    public void setCurrentMessage(Message message) {
        if(message == null) {
            messagePostDateLabel.setText("");
            messageTextArea.setText("");
            messageThreadIdLabel.setText("");
            messageUserIdLabel.setText("");
        } else {
            messagePostDateLabel.setText(message.getPostDate().toString());
            messageTextArea.setText(message.getText());
            messageThreadIdLabel.setText(message.getThreadId().toString());
            messageUserIdLabel.setText("" + message.getUserId());
        }
    }

    public void addToUserList(StringIntegerPair user) {
        usersComboBox.addItem(user);
    }

    public void addToThreadList(StringIntegerPair thread) {
        threadsComboBox.addItem(thread);
    }

    public void addToMessageList(StringIntegerPair message) {
        messagesComboBox.addItem(message);
    }

    public void setUserList(StringIntegerPair[] users) {
        usersComboBox.setItems(users);
        if(users.length == 0) setCurrentUser(null);
    }

    public void setThreadList(StringIntegerPair[] threads) {
        threadsComboBox.setItems(threads);
        if(threads.length == 0) setCurrentThread(null);
    }

    public void setMessageList(StringIntegerPair[] messages) {
        messagesComboBox.setItems(messages);
        if(messages.length == 0) setCurrentMessage(null);
    }

    public void replaceUser(StringIntegerPair currentUser, StringIntegerPair[] users) {
        setUserList(users);
        usersComboBox.setSelectedItem(currentUser);
    }

    public void replaceMessage(StringIntegerPair currentMessage, StringIntegerPair[] messages) {
        setMessageList(messages);
        messagesComboBox.setSelectedItem(currentMessage);
    }

    public void replaceThread(StringIntegerPair currentThread, StringIntegerPair[] threads) {
        setThreadList(threads);
        threadsComboBox.setSelectedItem(currentThread);
    }

    public void showConnectionClosedDialog() {
        JOptionPane.showMessageDialog(this, "Connection to server is closed.\nThe program will be terminated (ha ha ha).");
        System.exit(0);
    }
}
